<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    width="40%"
    :maskClosable="false"
    :title="t(`common.title.${type}`)"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
    <div style="margin-bottom: 25px; margin-left: 30px">
      目标项目
      <a-textarea
        v-model:value="targetDetail"
        style="width: 85%; margin-left: 5px"
        :auto-size="{ minRows: 2, maxRows: 6 }"
        :disabled="true"
      />
    </div>
    <div style="margin-bottom: 25px; margin-left: 30px">
      一级指标
      <a-textarea
        v-model:value="oneLevelDetail"
        style="width: 85%; margin-left: 5px"
        :auto-size="{ minRows: 2, maxRows: 6 }"
        :disabled="true"
      />
    </div>
    <div style="margin-bottom: 25px; margin-left: 30px">
      二级指标
      <a-textarea
        v-model:value="twoLevelDetail"
        style="width: 85%; margin-left: 5px"
        :auto-size="{ minRows: 2, maxRows: 6 }"
        :disabled="true"
      />
    </div>
    <div style="margin-bottom: 25px; margin-left: 30px">
      评分标准
      <a-textarea
        v-model:value="markingCriterion"
        style="width: 85%; margin-left: 5px"
        :auto-size="{ minRows: 2, maxRows: 6 }"
        :disabled="true"
      />
    </div>
    <div class="fen" style="margin-bottom: 25px; margin-left: 30px">
      <div>
        截止时间
        <a-input v-model:value="deadline" style="width: 200px; margin-left: 5px" :disabled="true" />
      </div>
      <div>
        二级分值
        <a-input
          v-model:value="twoLevelScore"
          style="width: 200px; margin-left: 5px"
          :disabled="true"
        />
      </div>
    </div>
    <div class="cailiao" v-if="targetTypes != 30">
      <Divider orientation="left" style="font-size: 14px">已提交材料</Divider>
      <table
        v-show="resultAll && Object.keys(resultAll).length > 0"
        align="center"
        border="1"
        class="tableName"
        cellpadding="10"
        cellspacing="5"
      >
        <!-- <caption>
          <a-button
            style="float: right"
            preIcon="ant-design:download-outlined"
            type="primary"
            @click="handleBatchDownload(resultAll)"
          >
            下载全部上传材料
          </a-button>
        </caption> -->
        <thead>
          <tr>
            <th>文件名称</th>
            <th>上传时间</th>
            <th>预览材料</th>
            <!-- <th>下载材料</th> -->
            <th v-if="upLoadShow != '1' && state">删除材料</th>
          </tr></thead
        >
        <tbody>
          <tr v-for="(item, value) in resultAll" :key="value">
            <td>{{ item.originalFileName }}</td>
            <td>{{ item.createdTime }}</td>
            <td>
              <a-button
                preIcon="ant-design:eye-filled"
                class="anniu anniu-first"
                @click="handleView(item.url)"
                >预览</a-button
              ></td
            >
            <!-- <td
              ><a-button
                preIcon="ant-design:download-outlined"
                class="anniu"
                @click="handleDown(item.fileId)"
                >下载</a-button
              ></td
            > -->
            <td
              ><a-button
                preIcon="ant-design:delete-outlined"
                class="anniu"
                @click="batchDelete(item.id)"
                v-if="upLoadShow != '1' && state"
                >删除</a-button
              ></td
            >
            <!-- v-hasAnyPermission="['del:upload']" -->
            <!-- v-if="upLoadShow != '1'" -->
            <!-- v-hasAnyPermission="['del:upload']" -->
          </tr>
        </tbody>
      </table>
    </div>
    <div>
      <BasicForm @register="registerForms" />
      <div
        style="margin-bottom: 25px; margin-left: 30px; margin-top: 10px"
        v-if="targetTypes != 30"
      >
        自我评分
        <a-input
          v-model:value="zipingfenshu"
          style="width: 200px; margin-left: 5px"
          :disabled="true"
        />
        <RadioGroup
          v-model:value="value1"
          button-style="solid"
          style="margin-left: 10px"
          :disabled="true"
        >
          <RadioButton value="A">A</RadioButton>
          <RadioButton value="B">B</RadioButton>
          <RadioButton value="C">C</RadioButton>
          <RadioButton value="D">D</RadioButton>
        </RadioGroup>
      </div>
    </div>
    <div style="margin-bottom: 25px; margin-left: 30px" v-if="targetTypes !== '30'">
      完成情况
      <a-textarea
        v-model:value="zipingfenshuneirong"
        style="width: 85%; margin-left: 5px"
        :auto-size="{ minRows: 2, maxRows: 6 }"
        :disabled="true"
      />
    </div>
    <div
      style="margin-bottom: 25px; margin-left: 30px; margin-top: 10px"
      v-if="targetTypes !== '30'"
    >
      最终评分
      <a-input
        v-model:value="pinweifenshu"
        style="width: 200px; margin-left: 5px"
        :disabled="true"
      />
      <RadioGroup
        v-model:value="value2"
        style="margin-left: 10px"
        button-style="solid"
        :disabled="true"
      >
        <RadioButton value="A">A</RadioButton>
        <RadioButton value="B">B</RadioButton>
        <RadioButton value="C">C</RadioButton>
        <RadioButton value="D">D</RadioButton>
      </RadioGroup>

      <div v-if="showTooltip" class="tooltip">这是用户名的注释</div>
    </div>
    <div style="margin-bottom: 25px; margin-left: 30px" v-if="targetTypes !== '30'">
      评分依据
      <a-textarea
        v-model:value="pinweifenshuneirong"
        style="width: 85%; margin-left: 5px"
        :disabled="true"
        :auto-size="{ minRows: 2, maxRows: 6 }"
      />
    </div>

    <div style="margin-top: 30px"
      ><a-button
        type="primary"
        v-hasAnyPermission="['taskInterim:assessMarks']"
        style="margin-left: 40px"
        v-show="targetTypes !== '30'"
        @click="pingfentanchuang"
        >最终评分
        <!-- v-if="targetTypes != 30" -->
      </a-button>
      <a-button
        type="primary"
        style="margin-left: 40px"
        @click="zipingtanchuang"
        v-hasAnyPermission="['taskInterims:markOperation']"
        v-if="upLoadShow != '1' && !isExpiration && targetTypes !== '30'"
        :disabled="!state"
        >我要自评
        <!-- && targetTypes != 30 -->
      </a-button>
      <a-button
        v-if="upLoadShow != '1' && isExpiration && targetTypes !== '30'"
        type="default"
        :disabled="true"
        >自评时间已截止</a-button
      >
      <!-- <a-button
        v-if="upLoadShow != '1' && targetTypes !== '30'"
        type="primary"
        v-hasAnyPermission="['open:pingfen']"
        style="margin-left: 40px"
        @click="chakan"
        >查看评分</a-button
      > -->
      <!-- && targetTypes != 30 -->
    </div>
    <div style="margin-top: 30px" v-if="targetTypes == '30'">
      招生宣传得分
      <a-input
        v-model:value="xuanchuan.xuanchuanScore"
        style="width: 200px; margin-left: 5px"
        :disabled="true"
      />
      招生任务完成得分
      <a-input
        v-model:value="xuanchuan.finishScore"
        style="width: 200px; margin-left: 5px"
        :disabled="true"
      />
    </div>
    <div style="margin-top: 30px" v-if="targetTypes == '30'">
      招生目标得分
      <a-input
        v-model:value="xuanchuan.zhaoshengTargetScore"
        style="width: 200px; margin-left: 5px"
        :disabled="true"
      />
      招生排名
      <a-input
        v-model:value="xuanchuan.ranking"
        style="width: 200px; margin-left: 5px"
        :disabled="true"
      />
    </div>
    <!-- <BasicForm @register="registerGroup" @submit="handleSubmit" /> -->
    <pinfenEdit @register="pinfenModal" @success="pinfenSuccess" />
    <zipingEdit @register="zipingModal" @success="zipingSuccess" />
  </BasicDrawer>
</template>
<script lang="ts">
  import { Divider, Radio } from 'ant-design-vue';
  import { defineComponent, ref } from 'vue';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { ActionEnum, FileBizTypeEnum } from '/@/enums/commonEnum';
  import { listByBizId } from '/@/api/lamp/file/upload';
  import { editFormSchema, editFormSchemas } from './defApplication.data';
  import { UploadgetDetailById, remove } from '/@/api/basic/qimo/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { downloadFile } from '/@/utils/lamp/common';
  import { downloadFiles } from '/@/api/basic/import/index';
  // import { number } from 'vue-types';
  import { useUserStore } from '/@/store/modules/user';
  import pinfenEdit from './pingfen.vue';
  import zipingEdit from './ziping.vue';
  import { useModal } from '/@/components/Modal';
  // import Icon from '/@/components/Icon';
  export default defineComponent({
    name: 'BasicApplicationEdit',
    components: {
      BasicDrawer,
      BasicForm,
      Divider,
      RadioGroup: Radio.Group,
      RadioButton: Radio.Button,
      pinfenEdit,
      zipingEdit,
    },
    props: {
      foundId: {
        type: String as PropType<string>,
        required: true,
      },
      opednext: {
        type: [Boolean] as PropType<boolean>,
        default: true,
      },
    },
    emits: ['success', 'register'],
    setup(props, { emit }) {
      // console.log(props.opednext);
      const [pinfenModal, { openModal: openPingfen }] = useModal();
      const [zipingModal, { openModal: openZiping }] = useModal();
      const { t } = useI18n();
      const type = ref<ActionEnum>(ActionEnum.VIEW);
      const value1 = ref<string>('A');
      const value2 = ref<string>('A');
      let showTrue = ref(true);
      let showlast = ref(false);
      let targetTypes: any = ref();
      const showTooltip = ref(false);
      let xuanchuan = {
        xuanchuanScore: '',
        finishScore: '',
        zhaoshengTargetScore: '',
        ranking: '',
      };
      let resultAll: any = ref();
      let opednext = ref(Boolean);
      let markResultVOList: any = ref([]);
      let zipingfenshu: any = ref();
      let pinweifenshu: any = ref();
      let zipingfenshuneirong: any = ref();
      let pinweifenshuneirong: any = ref();
      let targetDetail: any = ref();
      let oneLevelDetail: any = ref();
      let twoLevelDetail: any = ref();
      let markingCriterion: any = ref();
      let deadline: any = ref();
      let twoLevelScore: any = ref();
      let result: any = ref();
      const detailResule: any = ref();
      const userStore = useUserStore();
      const upLoadShow = userStore.getTenantId;
      const { createMessage } = useMessage();
      let results: any = ref();
      let isExpiration = ref(true);
      let state = ref(true);
      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        await resetSchema(editFormSchema(type));
        await resetFields();

        setDrawerProps({ confirmLoading: false });
        type.value = data?.type;

        // 赋值
        const record = { ...data?.record };
        const opednexts = { ...data };
        opednext.value = opednexts.opednext;
        results.value = record;
        showTrue.value = record.state;
        showlast.value = false;
        let arr = {};
        if (record.twoLevelTargetId) {
          arr = {
            otherId: [record.tenantId][0],
            taskTargetTwoId: [record.twoLevelTargetId][0],
          };
        } else {
          arr = {
            otherId: [record.tenantId][0],
            taskTargetTwoId: [record.id][0],
          };
        }

        await UploadDel(arr);
        const appendixIcons = await listByBizId({
          bizId: record.id,
          bizType: FileBizTypeEnum.DEF_APPLICATION_LOGO,
        });
        record.appendixIcon = appendixIcons?.[0]?.id;
        // listAll = record;
        await setFieldsValue(record);
        // return opednext;
      });
      const [registerForm, { setFieldsValue, resetFields, resetSchema }] = useForm({
        labelWidth: 100,
        schemas: editFormSchema(type),
        showActionButtonGroup: false,
        // disabled: showTrue,
        disabled: true,
        baseColProps: { span: 24 },
        actionColOptions: {
          span: 23,
        },
      });
      const [registerForms] = useForm({
        labelWidth: 100,
        schemas: editFormSchemas(type),
        showActionButtonGroup: false,
        // disabled: showTrue,
        disabled: true,
        baseColProps: { span: 24 },
        actionColOptions: {
          span: 23,
        },
      });
      async function UploadDel(value) {
        result.value = await UploadgetDetailById(value);
        detailResule.value = result.value;
        targetDetail.value = result.value.taskTargetOne.taskTarget.targetDetail;
        oneLevelDetail.value = result.value.taskTargetOne.oneLevelDetail;
        deadline.value = result.value.taskTargetOne.taskTarget.deadline;
        twoLevelScore.value = result.value.twoLevelScore;
        twoLevelDetail.value = result.value.twoLevelDetail;
        markingCriterion.value = result.value.markingCriterion;

        targetTypes.value = result.value.targetType;
        resultAll.value = result.value.materialResultVOList;
        isExpiration.value = result.value.isExpiration;
        state.value = result.value.state;
        xuanchuan.value = result.value.taskTargetMark;
        markResultVOList.value = result.value.taskTargetMark;
        if (result.value.taskTargetMark) {
          value1.value = markResultVOList.value.selfLevel;
          zipingfenshu.value = markResultVOList.value.selfScore;
          zipingfenshuneirong.value = markResultVOList.value.selfRemark;
          pinweifenshu.value = markResultVOList.value.finalScore;
          pinweifenshuneirong.value = markResultVOList.value.finalRemark;
          value2.value = markResultVOList.value.finalLevel;
        } else {
          zipingfenshu.value = undefined;
          pinweifenshu.value = undefined;
          zipingfenshuneirong.value = undefined;
          pinweifenshuneirong.value = undefined;
          value1.value = 'A';
          value2.value = 'A';
          // zipinshow.value = false;
          // pinweishow.value = false;
        }
        return {
          result,
          resultAll,
          markResultVOList,
          zipingfenshu,
          xuanchuan,
          pinweifenshu,
          zipingfenshuneirong,
          pinweifenshuneirong,
          targetTypes,
          isExpiration,
          state,
          value1,
          value2,
          // zipinshow,
          // pinweishow,
        };
      }
      // async function names(params) {
      //   const resule = await save(params);
      //   console.log(resule);
      // }
      function handleSubmit() {
        closeDrawer();
        showlast.value = false;
        emit('success');
      }
      // 预览
      function handleView(value) {
        window.open(value, '_blank');
      }
      // 下载
      async function handleDown(id: string[]) {
        // const response = await download(id);
        const response = await downloadFiles(id);
        downloadFile(response);
        createMessage.success(t('common.tips.downloadSuccess'));
      }
      async function handleBatchDownload(values: any[]) {
        const ids: any[] = [];
        for (var i = 0; i < values.length; i++) {
          ids.push(values[i].fileId);
        }
        const response = await downloadFiles(ids);
        downloadFile(response);
        createMessage.success(t('common.tips.downloadSuccess'));
      }
      // 删除
      async function batchDelete(id: string[]) {
        const delId = [id];
        await remove(delId);
        createMessage.success(t('common.tips.deleteSuccess'));
        closeDrawer();
        // let arr = {
        //   id: [record.id][0],
        //   otherId: [record.tenantId][0],
        // };
        // await UploadgetDetailById(arr)
      }
      function pingfentanchuang() {
        // console.log(detailResule, results, '11111111111111');
        openPingfen(true, {
          detailResule,
          results,
        });
      }
      function zipingtanchuang() {
        openZiping(true, {
          detailResule,
        });
      }
      function pinfenSuccess() {
        closeDrawer();
      }
      function zipingSuccess() {
        closeDrawer();
      }
      function chakan() {
        showlast.value = true;
      }
      return {
        chakan,
        showlast,
        showTooltip,
        result,
        pinfenModal,
        zipingModal,
        pingfentanchuang,
        zipingtanchuang,
        pinfenSuccess,
        zipingSuccess,
        t,
        registerDrawer,
        registerForm,
        registerForms,
        type,
        handleSubmit,
        UploadDel,
        resultAll,
        handleView,
        handleDown,
        xuanchuan,
        handleBatchDownload,
        batchDelete,
        remove,
        upLoadShow,
        markResultVOList,
        zipingfenshu,
        pinweifenshu,
        zipingfenshuneirong,
        pinweifenshuneirong,
        deadline,
        twoLevelScore,
        targetDetail,
        oneLevelDetail,
        markingCriterion,
        twoLevelDetail,
        showTrue,
        targetTypes,
        isExpiration,
        state,
        value1,
        value2,
        detailResule,
        // zipinshow,
        // pinweishow,
      };
    },
  });
</script>
<style lang="less" scoped>
  .names {
    line-height: 40px;
    height: 40px;
  }

  .anniu {
    margin-right: 5px;
  }

  .anniu-first {
    margin-left: 20px;
  }

  .cailiao-title {
    display: flex;
  }

  .tableName {
    border: 1 solid #000;
  }

  .fen {
    display: flex;
  }

  .fen > div {
    margin-right: 30px;
  }

  .fen:nth-child(1) {
    flex: 1;
  }

  .fen:nth-child(2) {
    flex: 1;
  }

  .thiss {
    width: 20px;
    height: 20px;
  }
</style>
